package com.sduept.nwld.dataserver.controller.daily;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

import org.primefaces.event.SelectEvent;
import org.springframework.beans.factory.annotation.Autowired;

import com.sduept.bigdata.fault.protection.model.ProtectChannelAlarm;
import com.sduept.bigdata.fault.protection.service.ProtectChannelAlarmQueryManager;
import com.sduept.utils.DateUtil;

@Named
@ViewScoped
public class ProtectChannelAlarmController extends DailyReportAbstractController {

	private static final long serialVersionUID = -1276959037712188496L;

	@Autowired
	private ProtectChannelAlarmQueryManager pc;
	private List<ProtectChannelAlarm> protectChannelAlarms = new ArrayList<>();

	@PostConstruct
	public void init() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String start = format.format(queryDate1);
		String end = format.format(queryDate2);
		protectChannelAlarms = pc.getAllProtectChannelAlarm(
				DateUtil.getDateByString(start, DateUtil.pattern_ymdhms),
				(DateUtil.getDateByString(end, DateUtil.pattern_ymdhms)));
	}
	public void refreshPage(){
		initParamter();
		init();
	}

	public void onDateSelectStart(SelectEvent popup11) {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		facesContext.addMessage(
				null,
				new FacesMessage(FacesMessage.SEVERITY_INFO, "Date Selected", format.format(popup11
						.getObject())));
	}

	public void onDateSelectEnd(SelectEvent popup12) {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		facesContext.addMessage(
				null,
				new FacesMessage(FacesMessage.SEVERITY_INFO, "Date Selected", format.format(popup12
						.getObject())));
	}

	public void queryByTime() throws ParseException {
		if (queryDate2 == null) {
			queryDate2 = new Date();
		}
		if (queryDate1 == null) {
			queryDate1 = new Date(0);
		}
		queryDate2 = DateUtil.addDays(queryDate2, 1);
		queryDate2 = DateUtil.addSeconds(queryDate2, -1);
		protectChannelAlarms = pc.getAllProtectChannelAlarm(queryDate1, queryDate2);
		refreshQueryParamter();
	}

	public void resetQuery(){
		queryDate1 = null;
		queryDate2 = null;
		resetqueryDate();
	}
	
	public List<ProtectChannelAlarm> getProtectChannelAlarms() {
		return protectChannelAlarms;
	}

	public void setProtectChannelAlarms(List<ProtectChannelAlarm> protectChannelAlarms) {
		this.protectChannelAlarms = protectChannelAlarms;
	}

}
